package com.algo.config.db;

import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;

import javax.sql.DataSource;

/**
 * 动态数据源
 */
public class MyRoutingDataSource extends AbstractRoutingDataSource {

    private static ThreadLocal<DBTypeEnum> CONETXT_HOLDER = new ThreadLocal<>();

    @Override
    protected Object determineCurrentLookupKey() {
        return getDataSource();
    }

    public DBTypeEnum getDataSource() {
        return CONETXT_HOLDER.get();
    }

    public static void setDataSource(DBTypeEnum dbTypeEnum) {
        CONETXT_HOLDER.set(dbTypeEnum);
    }

    public static void clearDataSource() {
        CONETXT_HOLDER.remove();
    }

}
